package nju.software.tagmanager.controller;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nju.software.tagmanager.common.result.Result;
import nju.software.tagmanager.pojo.dto.ServiceQueryDTO;
import nju.software.tagmanager.pojo.entity.Category;
import nju.software.tagmanager.pojo.entity.Item;
import nju.software.tagmanager.pojo.entity.Tag;
import nju.software.tagmanager.service.CategoryService;
import nju.software.tagmanager.service.ItemService;
import nju.software.tagmanager.service.TagService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: raychou
 * @date: 2024-01-12 15:23
 * @description: 用于给其他系统提供接口
 */

@Api(tags = "标签系统接口")
@RestController
@RequestMapping("/api")
@Slf4j
public class ServiceAPI {

    @Resource
    private CategoryService categoryService;
    @Resource
    private ItemService itemService;

    @Resource
    private TagService tagService;

    @ApiOperation("标签查询接口")
    @PostMapping("/tags")
    public Result<List<String>> tags(ServiceQueryDTO query) {
        List<Tag> list = tagService.find(query);
        List<String> result = list.stream().map(Tag::getName).collect(Collectors.toList());
        return Result.success("查询标签成功", result);
    }

    @ApiOperation("类别查询接口")
    @PostMapping("/categories")
    public Result<List<String>> categories(ServiceQueryDTO query) {
        List<Category> list = categoryService.find(query);
        List<String> result = list.stream().map(Category::getName).collect(Collectors.toList());
        return Result.success("查询类别成功", result);
    }

    @ApiOperation("主体查询接口")
    @PostMapping("/items")
    public Result<List<String>> items(ServiceQueryDTO query) {
        List<Item> list = itemService.find(query);
        List<String> result = list.stream().map(Item::getName).collect(Collectors.toList());
        return Result.success("查询主体成功", result);
    }
}
